<?php

class BusinessController extends AdminController
{

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Business;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Business']))
		{
			$model->attributes=$_POST['Business'];
            $model->created_date = time();
            $model->updated_date = time();
			if($model->save())
				$this->redirect(array('index'));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Business']))
		{
			$model->attributes=$_POST['Business'];
			if($model->save())
				$this->redirect(array('index'));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

        //do not delete business permanently, just set its status to -1
//        $model=$this->loadModel($id);
//        $model->status = -1;
//        $model->save();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
	}

	/**
	 * Manages all models.
	 */
	public function actionIndex()
	{
		$model=new Business('search');
		$model->unsetAttributes();  // clear any default values

		if (isset($_GET['pageSize'])) {
			Yii::app()->user->setState('pageSizeBusiness',(int)$_GET['pageSize']);
			unset($_GET['pageSize']);
		}

		if(isset($_GET['Business']))
			$model->attributes=$_GET['Business'];

		$this->render('index',array(
			'model'=>$model,
		));
	}

    public function actionVerified()
    {
        $model=new Business('search');
        $model->unsetAttributes();  // clear any default values

        if (isset($_GET['pageSize'])) {
            Yii::app()->user->setState('pageSizeBusiness',(int)$_GET['pageSize']);
            unset($_GET['pageSize']);
        }

        if(isset($_GET['Business']))
            $model->attributes=$_GET['Business'];

        $this->render('verified',array(
            'model'=>$model,
        ));
    }

    public function actionPending()
    {
        $model=new Business('search');
        $model->unsetAttributes();  // clear any default values

        if (isset($_GET['pageSize'])) {
            Yii::app()->user->setState('pageSizeBusiness',(int)$_GET['pageSize']);
            unset($_GET['pageSize']);
        }

        if(isset($_GET['Business']))
            $model->attributes=$_GET['Business'];

        $this->render('pending',array(
            'model'=>$model,
        ));
    }

    public function actionUnverified()
    {
        $model=new Business('search');
        $model->unsetAttributes();  // clear any default values

        if (isset($_GET['pageSize'])) {
            Yii::app()->user->setState('pageSizeBusiness',(int)$_GET['pageSize']);
            unset($_GET['pageSize']);
        }

        if(isset($_GET['Business']))
            $model->attributes=$_GET['Business'];

        $this->render('unverified',array(
            'model'=>$model,
        ));
    }

    public function actionSearch()
    {
        $model=new Business;

        $this->render('search',array(
            'model'=>$model,
        ));
    }

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Business the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Business::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Business $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='business-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

    public function actionInsert(){
        $model=new Business();

        if(isset($_POST['Business']))
        {
            $model->attributes=$_POST['Business'];
            $model->status = 1;
//            $model->status_ready = 1;
            $model->created_date = time();
            $model->updated_date = time();
            if($model->validate())
            {
                // form inputs are valid, do something here
                if($model->save()){
                    $response = array(
                        "result"=>"OK",
                    );
                    echo json_encode($response);
                }
                else{
                    $response = array(
                        "result"=>"Fail",
                    );
                    echo json_encode($response);
                }
                return;
            }
        }
        $this->render('create',array('model'=>$model));
    }
}
